/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using MyPhotoIndex.Interfaces;
using System.IO;

namespace MyPhotoIndex.Utilities
{
    public class Logger : Singleton<Logger>
    {
        const string LOG_FILE = "myPhotoIndexLog.txt";
        private object m_syncLock = new object();

        public Logger()
        {
            try
            {
                lock (m_syncLock)
                {
                    String path = Path.Combine(ApplicationFolders.DefaultLogPath, LOG_FILE);
                    if (File.Exists(path) == false)
                    {
                        using (FileStream fs = File.Create(path))
                        {
                        }
                    }
                    else
                    {
                        FileInfo fi = new FileInfo(path);
                        if (fi.Length > 2000000)
                        {
                            using (FileStream fs = File.Create(path))
                            {
                            }
                        }
                    }
                }
            }
            catch { }
        }

        private void LogWrite(Exception ex)
        {
            try
            {
                LogWrite(ex.ToString());
            }
            catch { }
        }

        private void LogWrite(string info, Exception ex)
        {
            string logEntry = string.Format("Error info: {0}\nException info: {1}", info, ex.ToString());
            LogWrite(logEntry);
        }

        private void LogWrite(String data)
        {
            try
            {
                lock (m_syncLock)
                {
                    String path = Path.Combine(ApplicationFolders.DefaultLogPath, LOG_FILE);
                    using (StreamWriter sw = File.AppendText(path))
                    {
                        String text = String.Format("{0} - {1}", DateTime.Now.ToUniversalTime().ToString(), data);
                        sw.WriteLine(text);
                    }
                }
            }
            catch { }
        }

        public static void Log(string info)
        {
            Instance.LogWrite(info);
        }

        public static void Log(Exception ex)
        {
            Instance.LogWrite(ex);
        }

        public static void Log(string info, Exception ex)
        {
            Instance.LogWrite(info, ex);
        }
    }
}
